package com.xzl.time.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.xzl.time.pojo.Comment;
import com.xzl.time.pojo.vo.AddCommentParamVo;
import com.xzl.time.service.ICommentService;
import com.xzl.tool.util.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "评论管理")
@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    private ICommentService commentService;
    //根据id查询评论
    @GetMapping("/findById")
    @ApiOperation("根据id查询评论")
    public RespBean findById(String id) {
        Comment comment = commentService.findById(id);
        return RespBean.success(comment);
    }

        //
    @GetMapping
    @ApiOperation("查询所有")
    public RespBean findAll() {
        List<Comment> list = commentService.findAll();
        return RespBean.success(list);
    }

    //新增
    @PostMapping
    @ApiOperation("新增")
    public RespBean save(@RequestBody AddCommentParamVo comment) {
        AddCommentParamVo save = commentService.save(comment);
        return RespBean.success("新增成功！",save);
    }

    //修改
    @PutMapping
    @ApiOperation("修改")
    public RespBean update(@RequestBody Comment comment) {
        commentService.update(comment);
        return RespBean.success("修改成功！");
    }

    //删除
    @DeleteMapping
    @ApiOperation("删除")
    public RespBean deleteById(String id,String userName) {
        commentService.deleteById(id,userName);
        return RespBean.success("删除成功！");
    }

    //根据文章id查询评论列表
    @GetMapping("/findByarticleId")
    @ApiOperation("根据文章id查询评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId",value = "文章id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "current",value = "页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "size",value = "条数",required = true,paramType = "query")

    })
    public RespBean findByarticleId(int current,int size,String articleId) {
        PageInfo<Comment> byarticleId = commentService.findByarticleId(current, size, articleId);
        return RespBean.success( byarticleId);
    }

    //根据文章id查询评论列表
    @GetMapping("/findByCeilingCommentId")
    @ApiOperation("根据评论父id查询评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ceilingCommentId",value = "评论顶级id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "current",value = "页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "size",value = "条数",required = true,paramType = "query")
    })
    public RespBean findByParentId(int current,int size,String ceilingCommentId) {
        PageInfo<Comment> byParentId = commentService.findByCeilingCommentId(current, size, ceilingCommentId);
        return RespBean.success( byParentId);
    }

    @GetMapping("/fabulous")
    @ApiOperation("点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId",value = "评论id",required = true,paramType = "query")
    })
    public RespBean fabulous(String commentId){
        commentService.fabulous(commentId);
        return RespBean.success();

    }
}
